package com.newenergy.wisehome.gateway.device;


import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.config.AppManager;
import com.newenergy.wisehome.data.bean.BLDeviceInfo;
import com.newenergy.wisehome.data.bean.Family;
import com.newenergy.wisehome.data.ormlite.dao.BLDeviceInfoDao;
import com.newenergy.wisehome.gateway.adapter.BLDeviceInfoAdapter;
import com.newenergy.wisehome.gateway.manager.BLLocalFamilyManager;
import com.newenergy.wisehome.ui.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.broadlink.sdk.data.controller.BLDNADevice;

/**
 * A simple {@link Fragment} subclass.
 */
public class FamilyBLDeviceInfoFragment extends BaseFragment {

    @BindView(R.id.btn_refresh_device)
    Button mBtnRefreshDevice;
    @BindView(R.id.btn_update_device)
    Button mBtnUpdateDevice;

    private ArrayList<BLDeviceInfo> mData = new ArrayList<>();
    private BLDeviceInfoAdapter mAdapter;


    @BindView(R.id.rv_bl_device)
    EasyRecyclerView mRecyclerView;

    @BindView(R.id.tv_family_info)
    TextView mTvFamillyInfo;

    private String mBlFamilyId;
    private BLDNADevice mLocalDevice;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_family_bldevice_info;
    }

    @Override
    public void init(View rootView) {
        initRecyclerView();
        BLLocalFamilyManager familyManager = BLLocalFamilyManager.getInstance();
        mBlFamilyId = familyManager.mBLFamilyId;
        mLocalDevice = familyManager.mLocalDnaDevice;

    }

    private void initRecyclerView() {
        mAdapter = new BLDeviceInfoAdapter(getContext());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));

    }

    @Override
    public void onResume() {
        super.onResume();
        mData.clear();
        AppManager appManager = AppManager.getInstance(getContext());
        Family currFamily = appManager.mCurrFamily;
        mTvFamillyInfo.setText("名称：" + currFamily.getName() + "\n  id: " + currFamily.getFamilyId());
        BLDeviceInfoDao infoDao = new BLDeviceInfoDao(getContext());
        List<BLDeviceInfo> allBLDeviceInfo = infoDao.getFamilyBLDeviceInfos(currFamily.getFamilyId());
        mData.addAll(allBLDeviceInfo);
        mAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.btn_refresh_device, R.id.btn_update_device})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_refresh_device:
//                getBLFamilyDevices();
                break;
            case R.id.btn_update_device:
                break;
        }
    }
}
